﻿/**
* Smoke Tester Tool : Post deployment smoke testing tool.
* 
* http://www.stephenhaunts.com
* 
* This file is part of Smoke Tester Tool.
* 
* Smoke Tester Tool is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
* 
* Smoke Tester Tool is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* 
* See the GNU General Public License for more details <http://www.gnu.org/licenses/>.
* 
* Curator: Stephen Haunts
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;

namespace CommonCode.Reports.Writer
{
    public class HtmReport : IReportWriter
    {

        public void WriteReport(string fileName, List<ReportEntry> reportEntries)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            if (reportEntries.Count == 0)
            {
                throw new ArgumentNullException("reportEntries");
            }
            var reportBody = new StringBuilder();
            reportBody.AppendLine("<h1>Test Run Report</h1>");
            reportBody.AppendFormat("<table class=\"legend\"><tr><td></td><td>Total Tests<td/><td>{0}</td></tr><tr><td class=\"pass\"></td><td>Passed<td/><td>{1}</td></tr><tr><td class=\"fail\"></td><td>Failed<td/><td>{2}</td></tr></table>", reportEntries.Count, reportEntries.Where(e => e.Result).Count(), reportEntries.Where(e => !e.Result).Count());
            
            reportBody.AppendLine("<table>\n<thead><tr><th>Result</th><th>Test Name</th><th>Duration</th><th>Error Message</th></tr></thead>\n<tbody>");

            foreach (var entry in reportEntries)
            {
                reportBody.AppendLine(string.Format("<tr><td><span class=\"{0}\">{1}</span></td><td>{2}</td><td>{3:hh\\:mm\\:ss\\.fff}</td><td>{4}</td></tr>", entry.Result ? "pass" : "fail", entry.Result, entry.TestName, (entry.TestStopTime - entry.TestStartTime), entry.ErrorMessage));
            }
            reportBody.AppendLine("<tbody>\n</table>");

            var cssTemplate = "<style type=\"text/css\">"
                            + "h1{font-size:20px; display:inline-block;}body *{font-size:14px;} .legend{margin-bottom:30px;} .legend td{padding:10px; border:solid 1px transparent; font-size:18px; font-weight:400; } .legend td:first-child{width:20px;} .legend tr{border-bottom:dotted 1px #ccc;}  .legend tr:nth-child(even) {background: #fff}ul{list-style-type:none;padding: 0px;margin:0px; } ul li{padding:14px;display:block:font-weight:500;} li span{width:100px;} table{border-collapse:collapse; }th, td {padding:8px; text-align:left; border:solid 1px #000;} th{background-color:#ccc; text-transform:uppercase; color:#000; border-color:#000; } tr:nth-child(even) {background: #ccc} tr:nth-child(odd) {background: #fff} tr:nth-child(even) :hover, tr:nth-child(odd) :hover{background: #fcc;} tr th:first-child{text-align:center;}  tr td:first-child span{display:block; height:22px;width:22px;text-indent: 100%; white-space: nowrap; overflow: hidden;}"
                            + ".pass {background: url(\"\");  background-size: 20px 20px; background-repeat: no-repeat;  }"
                            + ".fail {background: url(\"\");  background-size: 20px 20px; background-repeat: no-repeat; }"
                            + ".disabled {background: url(\"\");  background-size: 20px 20px; background-repeat: no-repeat;  }"
                            + "</style>";
            var htmlTemplate = string.Format("<html>\n<head>\n<title>{0}</title>\n{1}\n</head>\n<body>\n{2}\n</body>\n</html>", "Smoke Test - HTML Report", cssTemplate, reportBody);

            using (var file = new StreamWriter(fileName))
            {
                file.WriteLine(htmlTemplate.ToString());
                file.Close();
            }
        }
        public string Extension { get { return "htm"; } }
        public string Code { get { return "RunWithHtmReport"; } }
        public override string ToString()
        {
            return "Html Report";
        }
    }
}
